/**
 * Project: apollo-base-dal
 * 
 * File Created at 2017年2月20日
 * 
 * Copyright 2015-2016 dx.com Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * DongXue software Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with dx.com.
 */
package com.dx.pf.dal.conf.rule;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.dx.pf.dal.conf.rule.entity.DBRuleConf;
import com.dx.pf.dal.conf.rule.entity.ShardRuleConf;
import com.dx.pf.dal.conf.rule.entity.TBRuleConf;
import com.dx.pf.dal.Logger;
import com.dx.pf.dal.conf.rule.RuleConstant;

/** 
* @ClassName: RuleConfigXML 
* @Description:分片规则配置Xml定义
* @author wuzhenfang(wzfbj2008@163.com)
* @date 2017年2月20日 下午5:17:25 
* @version V1.0 
*/
public class RuleConfigXML {

	private final static Logger logger = Logger.getLogger(RuleConfigXML.class);
			
	private List<ShardRuleConf> shardRuleConfList = new ArrayList<ShardRuleConf>();
	
	@SuppressWarnings("unchecked")
	public RuleConfigXML(String configFile){
		SAXReader reader = new SAXReader();
		try {
			Document document = reader.read(new RuleConfigurationResolver(configFile).getReadloaderRuleStream());
			Element root = document.getRootElement();
			List<Element> shardElements = root.selectNodes("//" + RuleConstant.rule_shard);
			for (Element shardElement : shardElements) {
				ShardRuleConf ShardRule = new ShardRuleConf();
				ShardRule.setRuleId(shardElement.attributeValue(RuleConstant.id));
				//---------------db---------------------
				List<DBRuleConf> dbRuleConfList = new ArrayList<DBRuleConf>();
				List<Element> dbRuleElements = shardElement.selectNodes(RuleConstant.datasource_rule);
				for (Element dbelement : dbRuleElements) {
					DBRuleConf dbRuleConf = new DBRuleConf();
					dbRuleConf.setLogicDBName(dbelement.attributeValue(RuleConstant.db_name));
					dbRuleConf.setLogicTBName(dbelement.attributeValue(RuleConstant.tb_name));
					dbRuleConf.setTbRuleField(dbelement.attributeValue(RuleConstant.tb_rule_field));
					dbRuleConf.setExpressionType(dbelement.attributeValue(RuleConstant.expression_type));
					dbRuleConf.setRuleExpression(dbelement.attributeValue(RuleConstant.ruleExpression));
					dbRuleConfList.add(dbRuleConf);
				}
				ShardRule.setDbRuleConfs(dbRuleConfList);
				//----------------tb---------------------
				List<TBRuleConf> tbRuleConfList = new ArrayList<TBRuleConf>();
				List<Element> tbRulesElements = shardElement.selectNodes(RuleConstant.table_rules);
				for (Element tbRuleelement : tbRulesElements) {
					List<Element> tbRuleElements = tbRuleelement.selectNodes(RuleConstant.table_rule);
					for (Element tbRuleElement : tbRuleElements) {
						TBRuleConf tbRuleConf = new TBRuleConf();
						tbRuleConf.setLogicDBName(tbRuleElement.attributeValue(RuleConstant.db_name));
						tbRuleConf.setLogicTBName(tbRuleElement.attributeValue(RuleConstant.tb_name));
						tbRuleConf.setTbRuleField(tbRuleElement.attributeValue(RuleConstant.tb_rule_field));
						tbRuleConf.setExpressionType(tbRuleElement.attributeValue(RuleConstant.expression_type));
						tbRuleConf.setRuleExpression(tbRuleElement.attributeValue(RuleConstant.ruleExpression));
						tbRuleConfList.add(tbRuleConf);
					}
				}
				ShardRule.setTbRuleConfs(tbRuleConfList);
				shardRuleConfList.add(ShardRule);
			}
		} catch (Exception e) {
			logger.error("加载分片规则配置文件错误。 rule conf file:"+configFile, e);
		}
	}

	/**
	 * @return the shardRuleConfList
	 */
	public List<ShardRuleConf> getShardRuleConfList() {
		return shardRuleConfList;
	}

	/**
	 * @param shardRuleConfList the shardRuleConfList to set
	 */
	public void setShardRuleConfList(List<ShardRuleConf> shardRuleConfList) {
		this.shardRuleConfList = shardRuleConfList;
	}
	
	
}
